package com.douyin_spider.webmagic.Processor;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.minidev.json.JSONObject;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Component
public class DouyinProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3);

    @Override
    public void process(Page page) {
        List<HashMap<String, String>> list = new ArrayList<>();
        if (page.getHeaders().get("Content-Type") != null && page.getHeaders().get("Content-Type").toString().contains("application/json")) {
            List<String> all = page.getJson().jsonPath("emoji_list").all();
            for (String s : all) {
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    JsonNode jsonNode = objectMapper.readTree(s);
                    String origin_uri = jsonNode.get("origin_uri").asText();
                    String display_name = jsonNode.get("display_name").asText();
                    String emoji_url = jsonNode.get("emoji_url").get("url_list").get(0).asText();
                    Request request = new Request(emoji_url);
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("origin_uri", origin_uri);
                    hashMap.put("display_name", display_name);
                    hashMap.put("emoji_url", emoji_url);
                    list.add(hashMap);
                    request.putExtra("map", hashMap);
                    page.addTargetRequest(request);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            // 处理图片
//            Request request = page.getRequest();
//            HashMap map = request.getExtra("map");
        }
        page.putField("list", list);
    }

    @Override
    public Site getSite() {
        site.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36");
        site.addHeader("referer", "https://www.douyin.com/");
        return site;
    }
}
